package org.docmanagement.commons.service;

import javax.servlet.ServletContext;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
// TODO A dezinguer
public class ServiceLocator
{
	private static ServiceLocator instance = null;
	
	public static void init(ActionServlet servlet, ModuleConfig config)
	{
		instance = new ServiceLocator(servlet, config);
	}

	public static ServiceLocator getInstance() {
		if (instance == null)
			throw new RuntimeException("Spring not initialized");
		return instance;
	}

	private WebApplicationContext wac = null;

	private ServiceLocator(ActionServlet actionServlet, ModuleConfig config) {
		super();
		ServletContext servletContext = actionServlet.getServletContext();
		wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	}
	
	public Object getService(String name)
	{
		return wac.getBean(name);
	}

}
